package ms.maplestory.boot.constants;

import lombok.extern.slf4j.Slf4j;
import ms.maplestory.boot.enums.BodyPartType;
import ms.maplestory.boot.enums.EquipPrefixType;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class ItemConstants {

    public static int getBodyPartFromItem(int itemId, int gender) {
        List<Integer> arr = getBodyPartArrayFromItem(itemId, gender);
        int result = arr.size() > 0 ? arr.get(0) : 0;
        log.debug("itemId:{},gender:{},pos:{}", itemId, gender, result);
        return result;
    }

    public static List<Integer> getBodyPartArrayFromItem(int itemId, int genderArg) {
        EquipPrefixType prefix = EquipPrefixType.getByValue(getItemPrefix(itemId));
        List<Integer> bodyPartList = new ArrayList<>();
        if (prefix != null) {
            switch (prefix) {
                case Cap:
                    bodyPartList.add(BodyPartType.Cap.getValue());
                    break;
                case FaceAccessory:
                    bodyPartList.add(BodyPartType.FaceAccessory.getValue());
                    break;
                case EyeAccessory:
                    bodyPartList.add(BodyPartType.EyeAccessory.getValue());
                    break;
                case Earrings:
                    bodyPartList.add(BodyPartType.Earrings.getValue());
                    break;
                case Coat:
                case LongCoat:
                    bodyPartList.add(BodyPartType.Coat.getValue());
                    break;
                case Pants:
                    bodyPartList.add(BodyPartType.Pants.getValue());
                    break;
                case Shoes:
                    bodyPartList.add(BodyPartType.Shoes.getValue());
                    break;
                case Glove:
                    bodyPartList.add(BodyPartType.Glove.getValue());
                    break;
                case Shield:
                    bodyPartList.add(BodyPartType.Shield.getValue());
                    break;
                case Cape:
                    bodyPartList.add(BodyPartType.Cape.getValue());
                    break;
                case Ring:
                    bodyPartList.add(BodyPartType.Ring1.getValue());
                    bodyPartList.add(BodyPartType.Ring2.getValue());
                    bodyPartList.add(BodyPartType.Ring3.getValue());
                    bodyPartList.add(BodyPartType.Ring4.getValue());
                    break;
                case Medal:
                    bodyPartList.add(BodyPartType.Medal.getValue());
                    break;
                case TamingMob:
                    bodyPartList.add(BodyPartType.TamingMob.getValue());
                    break;
                case Saddle:
                    bodyPartList.add(BodyPartType.Saddle.getValue());
                    break;
                case DragonCap:
                    bodyPartList.add(BodyPartType.DragonCap.getValue());
                    break;
                case DragonPendant:
                    bodyPartList.add(BodyPartType.DragonPendant.getValue());
                    break;
                case DragonWing:
                    bodyPartList.add(BodyPartType.DragonWing.getValue());
                    break;
                case DragonShoes:
                    bodyPartList.add(BodyPartType.DragonShoes.getValue());
                    break;
                case MachineEngine:
                    bodyPartList.add(BodyPartType.MachineEngine.getValue());
                    break;
                case MachineArm:
                    bodyPartList.add(BodyPartType.MachineArm.getValue());
                    break;
                case MachineLeg:
                    bodyPartList.add(BodyPartType.MachineLeg.getValue());
                    break;
                case MachineFrame:
                    bodyPartList.add(BodyPartType.MachineFrame.getValue());
                    break;
                case MachineTransistor:
                    bodyPartList.add(BodyPartType.MachineTransistor.getValue());
                    break;
                default:
                    if (ItemConstants.isWeapon(itemId)) {
                        bodyPartList.add(BodyPartType.Weapon.getValue());
                    } else {
                        log.debug("Unknown type? id = " + itemId);
                    }

                    break;
            }
        } else {
            log.debug("Unknown type? id = " + itemId);
        }
        return bodyPartList;
    }

    public static int getGenderFromId(int itemId) {
        int result;
        if (itemId / 1000000 != 1 && getItemPrefix(itemId) != 254
                || getItemPrefix(itemId) == 119 || getItemPrefix(itemId) == 168)
            return 2;
        switch (itemId / 1000 % 10) {
            case 0:
                result = 0;
                break;
            case 1:
                result = 1;
                break;
            default:
                return 2;
        }
        return result;
    }

    private static int getItemPrefix(int itemId) {
        return itemId / 10000;
    }

    public static boolean isEquip(int id) {
        return id / 1000000 == 1;
    }

    public static boolean isWeapon(int itemID) {
        return itemID >= 1210000 && itemID < 1600000;
    }
}
